package com.practice.car.cardataapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.practice.car.cardataapp.entity.CarSaleMonth;
import com.practice.car.cardataapp.entity.frontEnd.*;
import com.practice.car.cardataapp.mapper.CarSaleMonthDao;
import com.practice.car.cardataapp.service.CarSaleMonthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * car-data-app project
 *
 * @author ZhangMingXing
 * @date 2020/9/5
 */
@Service
public class CarSaleMonthServiceImpl extends ServiceImpl<CarSaleMonthDao,CarSaleMonth> implements CarSaleMonthService {
	@Override
	public List<Form> getSeries() {
		List<CarSaleMonth> carSaleMonths = this.list();
		carSaleMonths.sort((a,b)->{
			String[] aStrings = a.getMonth().split("-");
			String[] bStrings = b.getMonth().split("-");
			Integer aYear = Integer.valueOf(aStrings[0]);
			Integer bYear = Integer.valueOf(bStrings[0]);
			if (!Objects.equals(aYear, bYear)) {
				return aYear - bYear;
			} else {
				Integer aMonth = Integer.valueOf(aStrings[1]);
				Integer bMonth = Integer.valueOf(bStrings[1]);
				return aMonth - bMonth;
			}
		});
		SeriesUtil<CarSaleMonth> carSaleMonthSeriesUtil = new SeriesUtil<>();
		carSaleMonthSeriesUtil.setLegendSolver(e->e.getMonth().split("-")[0]);
		carSaleMonthSeriesUtil.setFormSolver(e->e.getSale());
		return carSaleMonthSeriesUtil.getSeries(carSaleMonths);
	}


}
